home *** CD-ROM | disk | FTP | other *** search
- #include <SetupA4.h>
-
- #include "ExternalInterface.h"
- #include "DialogUtilities.h"
-
- /* This extension is merely a modified version of the "Prefix/Suffix
- Lines" that comes with BBEdit. I wanted an extension that would
- allow me to comment out blocks of text in C code, this is the
- result, as the default strings reveal.
- The changes made are:
- * Separate strings for prefix and suffixes
- * Auto detection of insertion strings, rather than using
- buttons
- Known bugs:
- * Remove suffix does not check that the suffix truely exists
- * Processes the file in two passes, probably slowing things
- down
- Other possible bug:
- * I am not sure that I this will correctly update the
- thermometer in the Progress window, and I have not tested it
- Notes:
- These humble modifications were made by Benjamin Elijah Griffin who
- would not think of charging for them. Others are welcome to make
- further modifications, such as bug fixes.
- Sorry so few comments, but I only added what I needed myself.
- (Actually most of the comments were added with a early version of
- this extension. :^)
- */
-
- /*
- This external shows the use of some less trivial functionality.
-
- It puts up a dialog, and uses the text entered in the text field
- to prefix every line, optionally in the selection.
- */
-
- enum
- {
- sel_only = 3,
-
- insert,
- delete,
-
- beginning,
- end,
-
- prefix_str,
- suffix_str
- };
-
- static struct
- {
- Boolean insert;
- Boolean line_start;
- Boolean line_end;
- Boolean sel_only;
- Boolean pad;
-
- Str255 prefix_str;
- Str255 suffix_str;
- } prefix_info;
-
- static void maintain_buttons(DialogPtr d)
- {
- SetDlgCtl(d, insert, prefix_info.insert);
- SetDlgCtl(d, delete, ! prefix_info.insert);
-
- /* SetDlgCtl(d, beginning, prefix_info.line_start);*/
- /* SetDlgCtl(d, end, prefix_info.line_end);*/
-
- SetDlgCtl(d, sel_only, prefix_info.sel_only);
- }
-
- pascal void main(ExternalCallbackBlock *callbacks, WindowPtr w)
- {
- DialogPtr d;
- GrafPtr save_port;
-
- long sel_end, sel_start, first_char;
- long start_line, end_line;
- long old_start;
-
- short item;
-
- Handle text;
- char *s, *ss;
-
- RememberA0();
- SetUpA4();
-
- GetPort(&save_port);
-
- d = callbacks->CenterDialog(128);
- SetPort(d);
-
- callbacks->GetSelection(&sel_start, &sel_end, &first_char);
-
- prefix_info.insert = TRUE;
- prefix_info.line_start = TRUE;
- BlockMove("\p/* ", prefix_info.prefix_str, 4);
- BlockMove("\p */", prefix_info.suffix_str, 4);
-
- prefix_info.sel_only = sel_end != sel_start;
-
- XAbleDlgCtl(d, sel_only, prefix_info.sel_only);
-
- SetStrItem(d, prefix_str, prefix_info.prefix_str);
- SetStrItem(d, suffix_str, prefix_info.suffix_str);
-
- SelIText(d, prefix_str, 0, 255);
- /* SelIText(d, suffix_str, 0, 255); */
-
- do {
- maintain_buttons(d);
-
- ModalDialog(callbacks->StandardFilter, &item);
-
- switch (item)
- {
- case insert:
- case delete:
- prefix_info.insert = (item == insert);
- break;
-
- /* case beginning:*/
- /* prefix_info.line_start = (item == beginning);*/
- /* break;*/
- /**/
- /* case end:*/
- /* prefix_info.line_end = (item == end);*/
- /* break;*/
-
- case sel_only:
- prefix_info.sel_only = ! prefix_info.sel_only;
- break;
- }
- } while ((item != 1) && (item != 2));
-
- ReadStrItem(d, prefix_str, prefix_info.prefix_str);
- ReadStrItem(d, suffix_str, prefix_info.suffix_str);
-
- DisposDialog(d);
- SetPort(save_port);
-
- s = (char *)&prefix_info.prefix_str[0];
- ss = (char *)&prefix_info.suffix_str[0];
-
- if ((item == 1) * ((s[0] != 0)+(ss[0] != 0))) {
- start_line = 0;
- end_line = callbacks->GetLastLine();
-
- if (prefix_info.sel_only) {
- start_line = callbacks->GetLineNumber(sel_start);
- end_line = callbacks->GetLineNumber(sel_end);
- };
-
- callbacks->StartProgress("\pChanging Lines...", 2 * (end_line - start_line), FALSE);
- old_start = start_line;
-
- if (s[0] != 0)
- {
- text = callbacks->GetWindowContents(w);
-
- /* if (prefix_info.line_start) */
- /* {*/
- if (prefix_info.insert)
- {
- for ( ; start_line < end_line; start_line++) {
- callbacks->DoProgress((2 * start_line) - old_start);
-
- sel_start = callbacks->GetLinePos(start_line);
- callbacks->SetSelection(sel_start, sel_start, first_char);
- callbacks->Insert((char *)&s[1], s[0]);
- };
- }
- else /* !prefix_info.insert */
- {
- for ( ; start_line < end_line; start_line++) {
- callbacks->DoProgress((2 * start_line) - old_start);
-
- sel_start = callbacks->GetLinePos(start_line);
-
- if (callbacks->FindPattern(*text, sel_start + s[0] + 1, sel_start,
- &s[1], s[0], FALSE) >= 0)
- {
- callbacks->SetSelection(sel_start, sel_start + s[0], first_char);
- callbacks->Delete();
- }
- };
- /* }*/
- }
- } /* if s!=0 */
-
- if (ss[0] != 0)
- {
- text = callbacks->GetWindowContents(w);
-
- start_line = old_start;
-
- /* if (prefix_info.line_end) */
- /* {*/
- if (prefix_info.insert)
- {
- for ( ; start_line < end_line; start_line++) {
- callbacks->DoProgress(start_line - old_start);
-
- sel_start = callbacks->GetLinePos(start_line);
- sel_start = callbacks->GetLineEnd(sel_start);
-
- callbacks->SetSelection(sel_start, sel_start, first_char);
- callbacks->Insert((char *)&ss[1], ss[0]);
- };
- }
- else
- {
- for ( ; start_line < end_line; start_line++) {
- callbacks->DoProgress(start_line - old_start);
-
- sel_start = callbacks->GetLinePos(start_line);
- sel_end = callbacks->GetLineEnd(sel_start);
-
- if (callbacks->FindPattern(*text, sel_end, sel_end - ss[0],
- &ss[1], ss[0], FALSE))
- {
- callbacks->SetSelection(sel_end - ss[0], sel_end, first_char);
- callbacks->Delete();
- }
- };
- /* }*/
- }
-
- callbacks->DoneProgress();
- }
- };
-
- RestoreA4();
- }
-